package android.medicalcloud.com.yiapp.yilian.ui.fragment;

import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.cach.PreferceManager;
import android.medicalcloud.com.yiapp.base.fragment.BaseFragment;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.StringTools;
import android.medicalcloud.com.yiapp.yilian.configer.contance.Constance;
import android.medicalcloud.com.yiapp.yilian.configer.enums.HttpConfig;
import android.medicalcloud.com.yiapp.yilian.im.ac.ChatActivity;
import android.medicalcloud.com.yiapp.yilian.im.model.Conversation;
import android.medicalcloud.com.yiapp.yilian.im.model.MessageFactory;
import android.medicalcloud.com.yiapp.yilian.im.model.NomalConversation;
import android.medicalcloud.com.yiapp.yilian.im.model.TextMessage;
import android.medicalcloud.com.yiapp.yilian.logic.CommonController;
import android.medicalcloud.com.yiapp.yilian.model.BannerModel;
import android.medicalcloud.com.yiapp.yilian.model.BannersModel;
import android.medicalcloud.com.yiapp.yilian.model.ConfigModel;
import android.medicalcloud.com.yiapp.yilian.ui.view.AutoScrollViewPager;
import android.medicalcloud.com.yiapp.yilian.ui.view.MainPageIndicator;
import android.medicalcloud.com.yiapp.yilian.ui.view.adapter.BannerAdapter;
import android.medicalcloud.com.yiapp.yilian.utils.android.IntentTool;
import android.view.View;
import android.widget.TextView;

import com.tencent.TIMConversation;
import com.tencent.TIMConversationType;
import com.tencent.TIMGroupCacheInfo;
import com.tencent.TIMMessage;
import com.tencent.qcloud.presentation.presenter.ConversationPresenter;
import com.tencent.qcloud.presentation.viewfeatures.ConversationView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by wanggenlin on 17-5-18.
 */
public class HomeFragment extends BaseFragment implements ConversationView,
        ViewNetCallBack, BannerAdapter.ClickListener, View.OnClickListener {
    AutoScrollViewPager mainViewPager;
    MainPageIndicator indicator;
    public final static int BANNER_AUTO_SCROLL_INTERVAL = 3000;
    BannerAdapter bannerAdapter;
    TextView latestMessage;
    private List<Conversation> conversationList = new LinkedList<>();
    private ConversationPresenter presenter;
    @Override
    protected void initView(View view) {
        mainViewPager = (AutoScrollViewPager) view.findViewById(R.id.mainViewPager);
        indicator = (MainPageIndicator) view.findViewById(R.id.indicator);
        latestMessage = (TextView) view.findViewById(R.id.latestMessage);
        view.findViewById(R.id.main_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConfigModel model = CommonController.getInstance().getConfigModel();
                if (model == null) {
                    return;
                }
                if (StringTools.isNullOrEmpty(model.getSign())) {
                    return;
                }
                IntentTool.webview(getActivity(), model.getSign(), "签约医院与医生");
            }
        });
        view.findViewById(R.id.imLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChatActivity.navToChat(getActivity(), PreferceManager.getInsance().getValueBYkey("to_identifier"), TIMConversationType.C2C);
            }
        });
        view.findViewById(R.id.main_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ConfigModel model = CommonController.getInstance().getConfigModel();
                if (model == null) {
                    return;
                }
                if (StringTools.isNullOrEmpty(model.getAbout())) {
                    return;
                }

                IntentTool.webview(getActivity(), model.getAbout(), "了解医联");
            }
        });
        presenter = new ConversationPresenter(this);
        presenter.getConversation();

    }

    private void initPager() {
        bannerAdapter = new BannerAdapter(getActivity());
        bannerAdapter.setListener(this);
        mainViewPager.setAdapter(bannerAdapter);
        mainViewPager.setAutoScrollDurationFactor(3.0f);
        mainViewPager.setInterval(BANNER_AUTO_SCROLL_INTERVAL);
        mainViewPager.startAutoScroll();
        indicator.setViewPager(mainViewPager);
        banners = new ArrayList<>();


    }

    List<BannerModel> banners;


    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        CommonController.getInstance().ad(this);
        initPager();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fra_home;
    }

    public static HomeFragment newInstance() {
        HomeFragment homeFragment = new HomeFragment();
        return homeFragment;
    }

    @Override
    public void click(View view, int positon) {
        IntentTool.webview(getActivity(), bannerAdapter.getItem(positon).getRedirectUrl(), "");
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onConnectStart(Object o) {

    }

    @Override
    public void onConnectEnd() {

    }

    @Override
    public void onFail(Exception e, Object o, String reslut) {

    }

    @Override
    public void initView(List<TIMConversation> conversationList) {

    }

    @Override
    public void updateMessage(TIMMessage message) {
        if (message == null) {
            return;
        }
//        if (MessageFactory.getMessage(message) instanceof TextMessage){
//            latestMessage.setText(((TextMessage)message).getMessage().);
//        }
        NomalConversation conversation = new NomalConversation(message.getConversation());
        Iterator<Conversation> iterator = conversationList.iterator();
        while (iterator.hasNext()) {
            Conversation c = iterator.next();
            if (conversation.equals(c)) {
                conversation = (NomalConversation) c;
                iterator.remove();
                break;
            }
        }
        conversation.setLastMessage(MessageFactory.getMessage(message));
        latestMessage.setText(conversation.getLastMessageSummary());
        refresh();
    }

    @Override
    public void updateFriendshipMessage() {

    }

    @Override
    public void removeConversation(String identify) {

    }

    @Override
    public void updateGroupInfo(TIMGroupCacheInfo info) {

    }

    @Override
    public void refresh() {

    }

    @Override
    public void onData(Serializable result, int flag, boolean fromNet, Object o) {
        if (flag == HttpConfig.ad.getType()) {
            BannersModel model = (BannersModel) result;
            Logger.e("lengt = " + model.getBanner().size());
            banners = model.getBanner();
            bannerAdapter.setRes(banners);
            indicator.setSize(banners.size());
        }

    }
}
